#!/bin/bash

# 显示ZooKeeper集群状态
check_status() {
    echo "检查ZooKeeper集群状态..."
    echo -e "\n=== zookeeper1状态 ==="
    docker exec -it zookeeper1 zkServer.sh status
    echo -e "\n=== zookeeper2状态 ==="
    docker exec -it zookeeper2 zkServer.sh status
    echo -e "\n=== zookeeper3状态 ==="
    docker exec -it zookeeper3 zkServer.sh status
}

# 连接到特定节点
connect_to_node() {
    NODE=$1
    echo "连接到 $NODE..."
    docker exec -it $NODE zkCli.sh
}

# 重启ZooKeeper集群
restart_cluster() {
    echo "重启ZooKeeper集群..."
    cd $(dirname "$0")/../dev-ops && docker-compose down && docker-compose up -d
    echo "ZooKeeper集群已重启"
}

# 显示四字命令
four_letter_command() {
    NODE=$1
    CMD=$2
    echo "在 $NODE 上执行四字命令: $CMD"
    docker exec -it $NODE bash -c "echo $CMD | nc localhost 2181"
    echo
}

# 主菜单
main_menu() {
    while true; do
        echo -e "\nZooKeeper工具箱"
        echo "=================="
        echo "1) 连接到ZooKeeper节点"
        echo "2) 检查集群状态"
        echo "3) 重启ZooKeeper集群"
        echo "4) 执行四字命令"
        echo "5) 退出"
        echo

        read -p "请输入选择(1-5): " main_choice
        
        case $main_choice in
            1)
                connect_menu
                ;;
            2)
                check_status
                ;;
            3)
                restart_cluster
                ;;
            4)
                four_letter_menu
                ;;
            5)
                echo "退出"
                exit 0
                ;;
            *)
                echo "无效选择，请输入1-5之间的数字"
                ;;
        esac
    done
}

# 连接菜单
connect_menu() {
    echo -e "\n请选择要连接的ZooKeeper节点:"
    echo "1) zookeeper1 (端口: 2181)"
    echo "2) zookeeper2 (端口: 2182)"
    echo "3) zookeeper3 (端口: 2183)"
    echo "4) 返回主菜单"
    echo

    read -p "请输入选择(1-4): " connect_choice
    
    case $connect_choice in
        1)
            connect_to_node "zookeeper1"
            ;;
        2)
            connect_to_node "zookeeper2"
            ;;
        3)
            connect_to_node "zookeeper3"
            ;;
        4)
            return
            ;;
        *)
            echo "无效选择，请输入1-4之间的数字"
            connect_menu
            ;;
    esac
}

# 四字命令菜单
four_letter_menu() {
    echo -e "\n请选择要连接的ZooKeeper节点:"
    echo "1) zookeeper1 (端口: 2181)"
    echo "2) zookeeper2 (端口: 2182)"
    echo "3) zookeeper3 (端口: 2183)"
    echo "4) 返回主菜单"
    echo

    read -p "请输入选择(1-4): " node_choice
    
    NODE=""
    case $node_choice in
        1) NODE="zookeeper1" ;;
        2) NODE="zookeeper2" ;;
        3) NODE="zookeeper3" ;;
        4) return ;;
        *) 
            echo "无效选择，请输入1-4之间的数字"
            four_letter_menu
            return
            ;;
    esac
    
    echo -e "\n请选择要执行的四字命令:"
    echo "1) stat - 服务器状态和连接的客户端列表"
    echo "2) ruok - 服务器是否正常运行"
    echo "3) conf - 服务器配置"
    echo "4) cons - 所有连接列表"
    echo "5) envi - 服务器环境变量"
    echo "6) mntr - 监控服务器"
    echo "7) wchs - 监视点摘要"
    echo "8) wchp - 按路径监视点"
    echo "9) 返回"
    echo

    read -p "请输入选择(1-9): " cmd_choice
    
    CMD=""
    case $cmd_choice in
        1) CMD="stat" ;;
        2) CMD="ruok" ;;
        3) CMD="conf" ;;
        4) CMD="cons" ;;
        5) CMD="envi" ;;
        6) CMD="mntr" ;;
        7) CMD="wchs" ;;
        8) CMD="wchp" ;;
        9) four_letter_menu; return ;;
        *) 
            echo "无效选择，请输入1-9之间的数字"
            four_letter_menu
            return
            ;;
    esac
    
    four_letter_command $NODE $CMD
}

# 启动主菜单
main_menu 